package com.jgy.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jgy.mapper.TemplateViewMapper;
import com.jgy.pojo.vo.DpTemplateParameter;
import com.jgy.service.DpTemplateService;
import com.jgy.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author 姜光远
 * @date 2022/8/17 15:58
 */
@RestController
@RequiredArgsConstructor
public class JsonTemplate {

    final DpTemplateService dpTemplateService;

    final TemplateViewMapper templateViewMapper;

    @GetMapping(value = "/reload")
    public R<Object> reload() {
        dpTemplateService.init();
        return R.ok("初始化完成");
    }

    @GetMapping(value = "/config/{viewKey}")
    public R<JSONObject> configData(@PathVariable("viewKey") String viewKey) {
        return R.ok(dpTemplateService.callConfig(viewKey));
    }

    @RequestMapping(value = "/data/{viewKey}", method = {RequestMethod.GET, RequestMethod.POST})
    public R<PageInfo<Map<String, Object>>> searchData(@PathVariable("viewKey") String viewKey, @RequestBody DpTemplateParameter dpTemplateParameter) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        PageInfo<Map<String, Object>> pageInfo = dpTemplateService.call(viewKey, dpTemplateParameter);
        return R.ok(pageInfo);
    }

    @PostMapping("/testdata")
    public R getData(Map<String, String> params){
        List<Map<String, Object>> view = templateViewMapper.view11(params);
        return R.ok(view);
    }

//    @RequestMapping(value = "/chart/{viewKey}", method = {RequestMethod.GET, RequestMethod.POST})
//    public R<JSONObject> chartData(@PathVariable("viewKey") String viewKey, @RequestBody DpTemplateParameter dpTemplateParameter) {
//        return R.ok(dpTemplateService.callCharts(viewKey, dpTemplateParameter));
//    }

//    @RequestMapping(value = "/tree/{viewKey}", method = {RequestMethod.GET, RequestMethod.POST})
//    public R<List<TreeVo>> treeData(@PathVariable("viewKey") String viewKey) {
//        return dpTemplateService.callTree(viewKey);
//    }

}
